<template>
  <div class="toggle-button" @click="toggleCollapse(!isCollapse)">|||</div>
  <!--<h5 class="mb-2">Custom colors</h5>-->
  <el-menu
    active-text-color="#409eff"
    background-color="#333744"
    class="el-menu-vertical-demo"
    text-color="#fff"
    :collapse="isCollapse"
    :ollapse-transition="false"
    :default-active="activePath"
    router
    unique-opened
  >
    <!--一级菜单-->
    <el-sub-menu :index="item.id + ''" v-for="item in menuList" :key="item.id">
      <template #title>
        <i :class="iconsObj[item.id]"></i>
        <span>{{ item.authName }}</span>
      </template>
      <!--二级菜单-->

      <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState(subItem.path)">
        <template #title>
          <el-icon>
            <Menu/>
          </el-icon>
          <span>{{ subItem.authName }}</span>
        </template>
      </el-menu-item>
    </el-sub-menu>

  </el-menu>
</template>

<script>
  import { Location } from '@element-plus/icons-vue'
  import { Menu } from '@element-plus/icons-vue'

  export default {
    name: 'MyAside',
    data () {
      return {
        menuList: [],
        iconsObj: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-shangpin',
          '101': 'iconfont icon-danju',
          '102': 'iconfont icon-baobiao',
          '145': 'iconfont icon-baobiao',
        },
        // 动态激活的地址
        activePath: ''
      }
    },
    components: {
      Location, Menu,
    },
    created () {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
      // console.log(this.activePath)
    },
    methods: {
      // 获取左侧菜单列表
      async getMenuList () {
        const { data: res } = await this.$http.get('menus')
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.menuList = res.data
        console.log(res)
      },
      // 切换菜单的折叠和展开
      toggleCollapse (isCollapse) {
        this.$emit('update:isCollapse', isCollapse)
      },
      // 获取链接地址，保存链接的激活状态
      saveNavState(activePath){
        window.sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
      },
    },
    props: ['isCollapse'],

  }
</script>

<style lang="less" scoped>
  .iconfont {
    margin-right: 10px;
  }

  .el-menu {
    border-right: none;
  }

  .el-sub-menu i {
    color: #909399;
  }

  .toggle-button {
    background-color: #4a5064;
    color: white;
    font-size: 10px;
    text-align: center;
    letter-spacing: 0.1em;
    cursor: pointer;
  }

</style>